<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\ViewCount;

class RecordVisit implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务尝试次数
     *
     * @var int
     */
    public $tries = 5;

    /*
    * 参数
    */
    protected $data;
    
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $agent_id = 0;
        if($this->data['code']){
            $agent_id = decodeCode($this->data['code'])[1];
        }
        ViewCount::create([
            'code'=>$this->data['code']??'',
            'path'=>$this->data['path'],
            'url'=>$this->data['url'],
            'category'=>$this->data['category'],
            'ip'=>$this->data['ip'],
            'ua'=>$this->data['ua'],
            'openid'=>$this->data['openid'],
            'tips'=>$this->data['tips']??'',
            'agent_id'=>$agent_id
        ]);
    }
}
